package me.goldze.mvvmhabit.binding.viewadapter.recyclerview

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import me.jessyan.autosize.utils.AutoSizeUtils

/**
 * Created by goldze on 2017/6/16.
 */
object LineManagers {
    @JvmStatic
    @JvmOverloads
    fun both(borderHeight: Int = 1, borderColor: Int = 0): LineManagerFactory {
        return object : LineManagerFactory {
            override fun create(recyclerView: RecyclerView): RecyclerView.ItemDecoration {
                val context = recyclerView.context
                val borderHeight = AutoSizeUtils.dp2px(context, borderHeight.toFloat())
                return DividerLine(context, borderHeight, borderColor, DividerLine.LineDrawMode.BOTH)
            }
        }
    }

    @JvmStatic
    @JvmOverloads
    fun bothTopBottom(borderHeight: Int = 1, borderColor: Int = 0): LineManagerFactory {
        return object : LineManagerFactory {
            override fun create(recyclerView: RecyclerView): RecyclerView.ItemDecoration {
                val context = recyclerView.context
                val borderHeight = AutoSizeUtils.dp2px(context, borderHeight.toFloat())
                return DividerLine(context, borderHeight, borderColor, DividerLine.LineDrawMode.BOTH_TOP_BOTTOM)
            }
        }
    }

    @JvmStatic
    @JvmOverloads
    fun none(borderHeight: Int = 1, borderColor: Int = 0): LineManagerFactory {
        return object : LineManagerFactory {
            override fun create(recyclerView: RecyclerView): RecyclerView.ItemDecoration {
                val context = recyclerView.context
                val borderHeight = AutoSizeUtils.dp2px(context, borderHeight.toFloat())
                return DividerLine(context, borderHeight, borderColor, DividerLine.LineDrawMode.NONE)
            }
        }
    }

    @JvmStatic
    @JvmOverloads
    fun horizontal(borderHeight: Int = 1, borderColor: Int = 0): LineManagerFactory {
        return object : LineManagerFactory {
            override fun create(recyclerView: RecyclerView): RecyclerView.ItemDecoration {
                val context = recyclerView.context
                val borderHeight = AutoSizeUtils.dp2px(context, borderHeight.toFloat())
                return DividerLine(context, borderHeight, borderColor, DividerLine.LineDrawMode.HORIZONTAL)
            }
        }
    }

    @JvmStatic
    @JvmOverloads
    fun vertical(borderHeight: Int = 1, borderColor: Int = 0): LineManagerFactory {
        return object : LineManagerFactory {
            override fun create(recyclerView: RecyclerView): RecyclerView.ItemDecoration {
                val context = recyclerView.context
                val borderHeight = AutoSizeUtils.dp2px(context, borderHeight.toFloat())
                return DividerLine(context, borderHeight, borderColor, DividerLine.LineDrawMode.VERTICAL)
            }
        }
    }

    @JvmStatic
    fun grid(borderHeight: Int = 1): LineManagerFactory {
        return object : LineManagerFactory {
            override fun create(recyclerView: RecyclerView): RecyclerView.ItemDecoration {
                val context = recyclerView.context
                val borderHeight = AutoSizeUtils.dp2px(context, borderHeight.toFloat())
                return object : RecyclerView.ItemDecoration() {
                    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
                        outRect.right = borderHeight
                        outRect.bottom = borderHeight
                    }
                }
            }
        }
    }

    interface LineManagerFactory {
        fun create(recyclerView: RecyclerView): RecyclerView.ItemDecoration
    }
}